<template>
  <basic-container :btnFlag=true>
    <div class="card-border">
      <card-header :header-data="headerData"></card-header>
      <el-form
        label-width="130px"
        :model="subjectForm"
        style="padding-right: 30px;"
        :rules="planSubjectRules"
        ref="subjectForm"
      >
        <el-row>
          <el-col :span="22" style="margin-top: 27px">
            <el-form-item class="form-item" label="计划名称 :" prop="title">
              <el-input v-model="subjectForm.title" placeholder="请输入计划名称"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="22">
            <el-form-item class="form-item" label="计划说明 :" prop="description">
              <el-input
                type="textarea"
                :autosize="{ minRows: 4, maxRows: 4}"
                v-model="subjectForm.description"
                placeholder="请输入计划说明"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div style="margin-left: 14px;margin-bottom: -13px;font-weight: 600">
        <span>课题计划方案</span>
      </div>
      <div class="card-body">
        <avue-crud :data="subjectListData"
                   :option="tableoption"
                   :page="page"
                   :table-loading="tableLoading"
                   @on-load="getResultList"
                   @size-change="sizeChange"
                   @current-change="currentChange"
                   class="avuecrud">
          <template slot-scope="scope" slot="menu">
            <div class="menu-title" @click.stop="planFeedback(scope.row,scope.index)">
              <i class="iconfont iconview icon-primary"></i>
              <p class="menu-title-text">查看反馈</p>
            </div>
            <div class="menu-title" @click.stop="subjectOpinionDelete(scope.row,scope.index)">
              <i class="iconfont iconalter icon-warning"></i>
              <p class="menu-title-text">删除</p>
            </div>
          </template>
        </avue-crud>
      </div>
      <div slot="footer" class="btn-footer">
        <el-button type="success" @click="exportPlanFile()">导出方案</el-button>
        <el-button type="primary" @click="submitToEdit()">确 定</el-button>
        <el-button @click="cancel()">取 消</el-button>
      </div>
      <el-dialog title="选择牵头单位" :visible.sync="dialogVisibleEditDept" width="1000px">
        <div style="margin-left: 47px;margin-top: -18px;">
          <p style="font-weight: 600">当前牵头单位 :{{deptNameEditDept}}</p>
        </div>
        <el-form
          label-width="130px"
          :model="projectFormEditDept"
          style="padding-right: 30px;"
          :rules="infosrules"
          ref="projectFormEditDept"
        >
          <el-row>
            <el-col :span="16">
              <el-form-item label="牵头单位 :" prop="orgCode" class="form-item">
                <span style="font-size: 16px">{{projectFormEditDept.supportDeptName}}</span>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-button size="small" type="primary" icon="el-icon-plus" style="margin-left: 10px;"
                         @click="clickjointEditDept">选择牵头单位
              </el-button>
            </el-col>
          </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="comitYesEditDept()">确 定</el-button>
          <el-button @click="cancelEditDept()">取 消</el-button>
        </div>
      </el-dialog>
      <el-dialog title="选择配合单位" :visible.sync="dialogVisibleEditDeptassort" width="1000px">
        <div style="margin-left: 47px;margin-top: -18px;">
          <p style="font-weight: 600">当前配合单位 :{{deptNameEditDeptassort}}</p>
        </div>
        <el-form
          label-width="130px"
          :model="projectFormEditDeptassort"
          style="padding-right: 30px;"
          :rules="infosrulesassort"
          ref="projectFormEditDeptassort"
        >
          <el-row>
            <el-col :span="16">
              <el-form-item label="配合单位 :" prop="orgCode" class="form-item">
                <span style="font-size: 16px">{{projectFormEditDeptassort.deptNameEditDeptassort}}</span>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-button size="small" type="primary" icon="el-icon-plus" style="margin-left: 10px;"
                         @click="clickjointassortEditDept">选择配合单位
              </el-button>
            </el-col>
          </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="comitYesEditDeptassort()">确 定</el-button>
          <el-button @click="dialogVisibleEditDeptassort = false">取 消</el-button>
        </div>
      </el-dialog>
      <dept-select :visible="deptVisible" @getSelectInfo="getSelectInfo" @cancel="cancelDept"></dept-select>
      <dept-check :visible="supportDeptVisible" @getCheckInfo="getSupportDeptCheckInfo"
                  @cancel="cancelSupportDept"></dept-check>
    </div>
  </basic-container>
</template>

<script>
  import {getPlanSubjectList,getSubjectInfo,subjectEditDept,editSubjectPlan,exportSubjectAssignDept} from "@/api/subject";
  import {planSubjectRules} from "@/const/infoRules";
  import {filedownload} from "@/api/system";

  export default {
    name: "modifyPlan",
    created(){
      var self = this;
      window.subjectDetail = self.subjectDetail;
      window.editMainDept = self.editMainDept;
      window.editMainDeptassort = self.editMainDeptassort;
      window.editAssortDept = self.editAssortDept;
      this.subjectForm.title = this.$route.query.title;
      this.subjectForm.description = this.$route.query.description;
      this.putSubjectListData();
    },
    data(){
      return{
        headerData: {
          title: '修改计划'
        },
        planSubjectRules:planSubjectRules,
        subjectListData:[],
        page: {
          total: 0, // 总页数
          currentPage: 1, // 当前页数
          pageSize: 10 // 每页显示多少条
        },
        tableLoading:false,
        tableoption: {
          page: true,
          menuWidth: 220,
          addBtn: false,
          editBtn: false,
          addRowBtn: false,
          indexLabel: "序号",
          index: true,
          indexWidth:70,
          cellBtn: false,
          delBtn: false,
          columnBtn: false,
          refreshBtn: false,
          filterBtn: false,
          searchBtn: false,
          keyId: "id",
          menu:false,
          column: [
            {
              label: "课题名称",
              width: 250,
              prop: "title",
              cell: true
            },
            {
              label: "课题概述",
              width: 330,
              prop: "description",
              cell: true
            },
            {
              label: "拟牵头单位",
              prop: "deptName",
              cell: true,
              formatter: function (row, value, label, column) {
                if (row.deptName == null) {
                  value = '请选择'
                }
                return '<a style="cursor: pointer;text-decoration:underline;" onclick="editMainDept(' + row.subjectId + ')">' + value + '</a>';
              }
            },
            {
              label: "拟配合单位",
              prop: "supportDeptName",
              cell: true,
              formatter: function (row, value, label, column) {
                if (row.supportDeptName == null) {
                  value = '请选择'
                }
                return '<a style="cursor: pointer;text-decoration:underline;" onclick="editMainDeptassort('  + row.subjectId +  ')">' + value + '</a>';
              }
            },
          ]
        },
        subjectOpinionListData:[],
        pageOpinion: {
          total: 0, // 总页数
          currentPage: 1, // 当前页数
          pageSize: 10 // 每页显示多少条
        },
        tableLoadingOpinion:false,
        tableoptionOpinion: {
          page: true,
          menuWidth: 120,
          addBtn: false,
          editBtn: false,
          addRowBtn: false,
          indexLabel: "序号",
          index: true,
          indexWidth:70,
          cellBtn: false,
          delBtn: false,
          columnBtn: false,
          refreshBtn: false,
          filterBtn: false,
          searchBtn: false,
          keyId: "id",
          column: [
            {
              label: "计划名称",
              width: 150,
              prop: "nickname",
              cell: true
            },
            {
              label: "计划日期",
              width: 150,
              prop: "nickname",
              cell: true
            },
            {
              label: "反馈部门",
              width: 150,
              prop: "nickname",
              cell: true
            },
            {
              label: "反馈结果",
              width: 200,
              prop: "title",
              cell: true
            },
            {
              label: "反馈时间",
              prop: "type",
              cell: true,
            },
            {
              label: "采纳意见",
              prop: "type",
              cell: true,
            },
          ]
        },
        subjectForm:{
          title:'',
          description:'',
          achievementId:0,
          addSubjectIds:[],
          removeSubjectIds:[],
        },
        deptNameEditDept:'',
        deptNameEditDeptassort:'',
        projectFormEditDept: {    //牵头部门表单
          supportDeptName: '',
          deptId: 0,
          supportDeptNameassort: '',
          subjectId: 0,
          excludeNull:true,
        },
        projectFormEditDeptassort: {   //配合部门表单
          supportDeptName: '',
          supportDeptNameassort: '',
          supportDeptIds: '',
          subjectId: 0,
          excludeNull:true,
        },
        dialogVisibleEditDept: false,
        dialogVisibleEditDeptassort:false,
        deptVisible:false,
        supportDeptVisible:false,
      }
    },
    methods: {
      currentChange(currentPage) {
        var page = this.page;
        page.currentPage = currentPage;
        this.getResultListPage(page);
      },
      sizeChange(pageSize) {
        var page = this.page;
        page.pageSize = pageSize;
        this.getResultListPage(page);
      },
      getResultListPage(page) {
        getPlanSubjectList(Object.assign({
          current: page.currentPage,
          size: page.pageSize,
          planId:this.$route.params.id,
        })).then(response=>{
          this.subjectListData = response.data.data.records;
          this.page.total = response.data.data.total;
        })
      },
      putSubjectListData(){
        getPlanSubjectList(Object.assign({
          current: 1,
          size: 10,
          planId:this.$route.params.id,
        })).then(response=>{
          this.subjectListData = response.data.data.records;
          this.page.total = response.data.data.total;
        })
      },
      getResultList() {

      },
      searchChangeOpinion() {

      },
      refreshChangeOpinion() {

      },
      getResultListOpinion(page) {

      },
      exportPlanFile(){
        this.$confirm("确定要导出方案吗?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          exportSubjectAssignDept(this.$route.params.id).then(response => {
            var exportFileName = this.$route.query.achievementTitle + "课题计划方案.docx";
            this.download(response.data, exportFileName)
          });
        })
      },
      // 附件下载
      downloadFile(fileId, fileName) {
        filedownload(fileId).then(response => {
          if (response.status == 200) {
            this.download(response.data, fileName)
          }
        }).catch((error) => {
          this.$notify({
            title: "失败",
            message: "上传失败[" + error + "]",
            type: "error",
            duration: 2000
          });
        })
      },
      // 下载文件
      download(data, filename) {
        if (data == null) {

        } else {
          let url = window.URL.createObjectURL(new Blob([data]));
          let link = document.createElement('a');
          link.style.display = 'none';
          link.href = url;
          link.setAttribute('download', filename);
          document.body.appendChild(link);
          link.click()
        }
      },
      submitToEdit(){
        this.$confirm("确定要提交修改吗?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.subjectForm.achievementId = this.$route.query.achievementId;
          var query = {
            planId:this.$route.params.id,
            subjectForm:this.subjectForm,
          };
           editSubjectPlan(query).then(response =>{
             if(response.data.code == 0){
               this.$notify({
                 title: '成功',
                 message: '修改成功',
                 type: 'success',
                 duration: 2000
               });
               this.$router.go(-1)
             }
           })
        })
      },
      cancel(){
        this.$router.go(-1)
      },
      //配置牵头部门
      async editMainDept(subjectId) {
        this.projectFormEditDept.supportDeptName = '';
        this.projectFormEditDept.deptId = 0;  //改为0防止没发做验证
        await getSubjectInfo(subjectId).then(response => {
          this.deptNameEditDept = response.data.data.deptName;
        });
        this.projectFormEditDept.subjectId = subjectId;
        this.dialogVisibleEditDept = true;
      },
      //配置配合部门
      async editMainDeptassort(subjectId) {
        this.projectFormEditDeptassort.supportDeptIds = '';  //改为0防止没发做验证
        this.projectFormEditDeptassort.deptNameEditDeptassort = '';  //改为0防止没发做验证
        await getSubjectInfo(subjectId).then(response => {
          this.deptNameEditDeptassort = response.data.data.supportDeptName;
        });
        this.projectFormEditDeptassort.subjectId = subjectId;
        this.dialogVisibleEditDeptassort = true;
      },
      comitYesEditDept() {
        if (this.projectFormEditDept.deptId == 0) {
          this.$notify({
            title: '提示',
            message: '请选择牵头单位',
            type: 'warning',
            duration: 2000
          });
          return;
        }
        this.projectFormEditDept.excludeNull = true;
        subjectEditDept(this.projectFormEditDept).then(response => {
          if (response.data.code == 0) {
            this.$notify({
              title: '成功',
              message: '成功',
              type: 'success',
              duration: 2000
            });
            this.dialogVisibleEditDept = false;
            this.projectFormEditDept.supportDeptName = '';
            this.projectFormEditDept.deptId = 0;
            this.getResultListPage(this.page);
          }
        })
      },
      // 弹出协同部门弹框
      clickjointEditDept() {
        this.deptVisible = true
      },
      cancelDept(deptIds, deptNames) {
        this.deptVisible = false;
      },
      // 弹出协同部门弹框
      clickjointassortEditDept() {
        this.supportDeptVisible = true
      },
      getSelectInfo(deptId, deptName) {
        this.projectFormEditDept.supportDeptName = deptName;
        this.projectFormEditDept.deptId = deptId;
        this.deptVisible = false;
      },
      getSupportDeptCheckInfo(deptIds, deptNames) {
        this.projectFormEditDeptassort.deptNameEditDeptassort = deptNames;
        this.projectFormEditDeptassort.supportDeptIds = deptIds;
        this.supportDeptVisible = false;
      },
      cancelSupportDept(deptIds, deptNames) {
        this.supportDeptVisible = false;
      },
      comitYesEditDeptassort() {
        if (this.projectFormEditDeptassort.supportDeptIds == '') {
          this.$notify({
            title: '提示',
            message: '请选择配合单位',
            type: 'warning',
            duration: 2000
          });
          return;
        }
        this.projectFormEditDeptassort.excludeNull = true;
        subjectEditDept(this.projectFormEditDeptassort).then(response => {
          if (response.data.code == 0) {
            this.$notify({
              title: '成功',
              message: '成功',
              type: 'success',
              duration: 2000
            });
            this.dialogVisibleEditDeptassort = false;
            this.projectFormEditDeptassort = {};
            this.getResultListPage(this.page);
          }
        })
      },

    }

  }
</script>

<style lang="scss" scoped>
  .btn-footer{
    text-align: center;
    margin-bottom: 20px;
  }
  .form-item{
    font-weight: 600;
  }
</style>

